package info.motteke.kurumi.swt;

import info.motteke.kurumi.GuiFactory;
import info.motteke.kurumi.ImageWindow;
import info.motteke.kurumi.VoiceBlock;
import info.motteke.kurumi.VoiceDetail;
import info.motteke.kurumi.VoiceInput;
import info.motteke.kurumi.VoiceRelation;
import info.motteke.kurumi.VoiceTable;
import info.motteke.kurumi.Window;
import info.motteke.mixivoice4j.ImageUrl;
import info.motteke.mixivoice4j.Voice;
import info.motteke.util.MixiVoiceProperties;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;

public class SwtGuiFactory implements GuiFactory {

    private final SwtWindow swtWindow;

    private final SwtImageCache imageCache;

    private final SwtVoiceTable voiceTable;

    private final SwtVoiceDetail voiceDetail;

    private final SwtVoiceInput voiceInput;

    private final SwtVoiceRelation voiceRelation;

    public SwtGuiFactory() {
        swtWindow = new SwtWindow();
        imageCache = new SwtImageCache("image");

//        SashForm baseSash = createBaseSash();
//        Composite leftHalf = createLeftHalf(baseSash);
        Composite leftHalf = createLeftHalf(swtWindow.getShell());
        SashForm voiceSash = createVoiceSash(leftHalf);

        voiceTable = new SwtVoiceTable(voiceSash, imageCache);
        voiceTable.getTable().addControlListener(new VoiceSashResizeListener(voiceSash));

        voiceDetail = new SwtVoiceDetail(voiceSash, imageCache);
        voiceInput = new SwtVoiceInput(leftHalf);
//        voiceRelation = new SwtVoiceRelation(baseSash);
        voiceRelation = null;

        int rate = MixiVoiceProperties.getInstance().getWindowTableHeightRate();
        int[] rates = new int[]{rate, 100 - rate};
        voiceSash.setWeights(rates);

    }

    private SashForm createBaseSash() {
        SashForm sash = new SashForm(swtWindow.getShell(), SWT.HORIZONTAL);
        sash.setLayout(new FillLayout());

        return sash;
    }

    private Composite createLeftHalf(Composite base) {
        Composite c = new Composite(base, SWT.NONE);
        c.setLayout(new GridLayout(1, true));

        return c;
    }

    private SashForm createVoiceSash(Composite leftHalf) {
        SashForm sash = new SashForm(leftHalf, SWT.VERTICAL);
        sash.setLayout(new FillLayout());
        sash.setLayoutData(new GridData(GridData.FILL_BOTH));

        return sash;
    }

    @Override
    public Window getWindow() {
        return swtWindow;
    }

    @Override
    public VoiceTable getVoiceTable() {
        return voiceTable;
    }

    @Override
    public VoiceDetail getVoiceDetail() {
        return voiceDetail;
    }

    @Override
    public VoiceInput getVoiceInput() {
        return voiceInput;
    }

    @Override
    public VoiceRelation getVoiceRelation() {
        return voiceRelation;
    }

    @Override
    public SwtImageCache getImageCache() {
        return imageCache;
    }

    @Override
    public ImageWindow createImageWindow(String title, ImageUrl url) {
        return new SwtImageWindow(swtWindow.getShell(), title, url);
    }

    @Override
    public VoiceBlock createVoiceBlock(Voice voice) {
        return new SwtHistoryBlock(voiceRelation.getBase(), voice, getImageCache());
    }

    @Override
    public void close() {
        imageCache.dispose();
    }
}
